package com.vm.osm.business.controller;

import com.vm.osm.business.common.Constants;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ValidLogon;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.req.logic_equ.PageAssetEquReq;
import com.vm.osm.business.dto.req.logic_equ.GetAssetEquDetailReq;
import com.vm.osm.business.dto.req.logic_equ.SearchCustomerReq;
import com.vm.osm.business.dto.rsp.asset_equ.AssetEquDetail;
import com.vm.osm.business.dto.rsp.asset_equ.PageAssetEquRsp;
import com.vm.osm.business.dto.rsp.logic_equ.SearchCustomerRsp;
import com.vm.osm.business.entity.OsmLogicEqu;
import com.vm.osm.business.service.IAssetEquService;
import com.vm.osm.business.util.UserDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/v1.0/asset_equ")
@Api(value = "AssetEquController", description = "资产设备", tags = {"AssetEquController"})
public class AssetEquController extends ControllerExceptionHandler {
    private static Logger LOG = LoggerFactory.getLogger(AssetEquController.class);
    @Autowired
    private IAssetEquService assetEquService;

    /**
     * 分页查询资产设备信息
     */
    @ValidLogon
    @ApiOperation(value = "分页查询资产设备信息", response = PageAssetEquRsp.class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/page")
    public Object page(HttpServletRequest servlet,
                       @Valid @ModelAttribute PageAssetEquReq request, BindingResult valid) {
        request.setSession_id(servlet.getHeader(Constants.SESSION_ID));
        if (null == request.getBelong_type()
                || OsmLogicEqu.BelongType.SELF == request.getBelong_type()) {
            request.setBelonger_id(UserDataUtil.getUserSelectedEnterpriseId(servlet));
        }
        LOG.info("【资产设备】（分页查询设备信息） -> 请求参数：{}", request);
        if (valid.hasErrors()) {
            throw new BusinessException(valid.getFieldError().getDefaultMessage());
        }
        return assetEquService.page(request);
    }

    /**
     * 查询资产设备详情
     */
    @ValidLogon
    @ApiOperation(value = "查询资产设备详情", response = AssetEquDetail.class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/detail/{id}")
    public Object detail(HttpServletRequest servlet,
                         @ApiParam(name = "id", value = "设备Id", required = true) @PathVariable String id) {
        GetAssetEquDetailReq request = new GetAssetEquDetailReq();
        request.setId(id);
        request.setSession_id(servlet.getHeader(Constants.SESSION_ID));
        LOG.info("【资产设备】（查询设备详情） -> 请求参数：{}", request);
        return assetEquService.detail(request);
    }

    /**
     * 查询资产设备详情
     */
    @ValidLogon
    @ApiOperation(value = "查询资产设备客户", response = SearchCustomerRsp.class)
    @ApiImplicitParam(name = "session_id", value = "访问令牌", required = true, dataType = "string", paramType = "header")
    @GetMapping("/customer/search")
    public Object searchCustomer(HttpServletRequest servlet,
                                 @ApiParam(name = "customer_name", value = "客户名称")
                                 @RequestParam(required = false) String customer_name) {
        SearchCustomerReq request = new SearchCustomerReq();
        request.setCustomer_name(customer_name);
        request.setCompany_id(UserDataUtil.getUserSelectedEnterpriseId(servlet));
        LOG.info("【资产设备】（查询设备详情） -> 请求参数：{}", request);
        return assetEquService.searchCustomer(request);
    }
}
